package com.example.li.buttonview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.RectF;
import android.hardware.TriggerEvent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ToggleButton extends View {

    private boolean toggle;
    private float x;
    private float y;

    public ToggleButton(Context context) {
        super(context);
    }

    public ToggleButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        into(attrs);
    }

    private void into(AttributeSet attrs){
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ToggleButton);
        toggle = typedArray.getBoolean(R.styleable.ToggleButton_toggle, false);
        typedArray.recycle();
    }

    private int toggleWidth;
    private int toggleHeight;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        switch (modeWidth) {
            case MeasureSpec.EXACTLY:
                toggleWidth=sizeWidth;
                break;
            case MeasureSpec.AT_MOST:
                toggleWidth=200;
                break;
        }
        toggleHeight=sizeHeight;
        setMeasuredDimension(toggleWidth,toggleHeight);
    }

    private float buttonSize;

    @Override
    protected void onDraw(Canvas canvas) {
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        Bitmap background = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_background);
        Rect backgrounRrect = new Rect(0, 0, background.getWidth(), background.getHeight());
        RectF backgroundRectF = new RectF(0, 0, measuredWidth, measuredHeight);
        canvas.drawBitmap(background,backgrounRrect,backgroundRectF,null);

        Bitmap button = BitmapFactory.decodeResource(getResources(), R.mipmap.slide_button);
        Rect buttomRect = new Rect(0, 0, button.getWidth(), button.getHeight());
        RectF buttonRectF = new RectF(buttonSize, 0, measuredWidth/1.8f, measuredHeight);
        canvas.drawBitmap(button,buttomRect,buttonRectF,null);
    }

    private float nowX;
    private float nowY;
    private float lastX;
    private float lastY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getX();
        y = event.getY();
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                lastX=x;
                lastY=y;
                return true;
            case MotionEvent.ACTION_MOVE:
                buttonSize=x-lastX;
                nowY=y;
                return true;
            case MotionEvent.ACTION_UP:

                return true;
        }

        return true;
    }
}
